import store from '@/store'

export default {
  inserted(el, binding, vnode) {
    const { value } = binding
    // 对原来设计作了修改， roles 保存色,authorities保存授权
    // const roles = store.getters && store.getters.roles
    const authorities = store.getters && store.getters.authorities
    if (value && value instanceof Array && value.length > 0) {
      const permissionRoles = value

      const hasPermission = authorities.some(auth => {
        return permissionRoles.includes(auth)
      })

      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error(`need roles! Like v-permission="['user:add','user:edit']"`)
    }
  }
}
